package SmartMeeting.facades;

import SmartMeeting.Entity.Meeting;
import SmartMeeting.bridges.*;
import SmartMeeting.bridges.Controller.DesktopController;
import SmartMeeting.bridges.Controller.MeetingController;
import SmartMeeting.bridges.Controller.MobileController;
import SmartMeeting.bridges.Controller.WebController;
import SmartMeeting.builders.ConcreteMeetingBuilder;
import SmartMeeting.builders.Director;
import SmartMeeting.builders.MeetingBuilder;
import SmartMeeting.decorators.ConcreteMeetingRecord;
import SmartMeeting.decorators.KeyWordDecorator;
import SmartMeeting.decorators.MeetingRecord;
import SmartMeeting.factorys.ComponentFactory;
import SmartMeeting.factorys.Factory.*;
import SmartMeeting.factorys.*;
import SmartMeeting.strategy.PerformanceFirstStrategy;
import SmartMeeting.strategy.SchedulingStrategy;

import java.util.List;

public class MeetingSystem {
    private ComponentFactory componentFactory;
    private MeetingController meetingController;
    private NetManager netManager;
    private UI ui;
    private Notifier notifier;

    public MeetingSystem() {
        // 默认构造函数
    }

    //构造函数，根据类型创建不同的组件工厂
    public MeetingSystem(String type) {
        switch (type) {
            case "Desktop" -> {
                System.out.println("开始桌面会议");
                componentFactory = new DesktopComponent();//创建桌面组件
                this.netManager = componentFactory.createNetManager();
                this.meetingController = new DesktopController(new WindowsPlatform());
            }

            case "Mobile" -> {
                System.out.println("开始移动会议");
                componentFactory = new MobileComponent();//创建移动组件
                this.ui = componentFactory.createUI();
                this.meetingController = new MobileController(new MobilePlatform());
            }

            case "Web" -> {
                System.out.println("开始Web会议");
                componentFactory = new WebComponent();//创建Web组件
                this.notifier = componentFactory.createNotifier();
                this.meetingController = new WebController(new WebPlatform());
            }
        }
    }

    public Meeting createMeeting(String agent, String time, String place, List<String> participants) {
        MeetingBuilder meetingBuilder = new ConcreteMeetingBuilder();//创建会议
        Director director = new Director(meetingBuilder);//使用Director构建会议
        //构建会议
        return director.construct(agent, time, place, participants);
    }

    public void startMeeting(Meeting meeting, String type) {
        SchedulingStrategy schedulingStrategy = new PerformanceFirstStrategy();//默认性能优先策略
        schedulingStrategy.schedule(meeting);//先开始会议
        System.out.println();

        //再通过组件发送邀请
        switch (type) {
            case "Desktop" -> {
                netManager.sendInvitation("会议已开始", meeting.getParticipants());//发送会议邀请
            }

            case "Mobile" -> {
                ui.sendInvitation("会议已开始", meeting.getParticipants());//发送会议邀请
            }

            case "Web" -> {
                notifier.sendInvitation("会议已开始", meeting.getParticipants());//发送会议邀请
            }
        }
        System.out.println();

        meetingController.startMeetingTool(meeting.getId());
        System.out.println();
    }

    public void endMeeting(Meeting meeting, String type) {
        //会议结束后只需要通知会议结束和记录会议即可
        meetingController.endMeetingTool(meeting.getId());
        System.out.println();

        switch (type) {
            case "Desktop" -> {
                netManager.sendNotification("会议已结束", meeting.getParticipants());//发送结束通知

            }

            case "Mobile" -> {
                ui.sendNotification("会议已结束", meeting.getParticipants());//发送结束通知
            }

            case "Web" -> {
                notifier.sendNotification("会议已结束", meeting.getParticipants());//发送结束通知
            }
        }
        System.out.println();

        MeetingRecord meetingRecord = new ConcreteMeetingRecord();
        meetingRecord = new KeyWordDecorator(meetingRecord);//添加关键词
        meetingRecord.record(meeting);//记录会议
        System.out.println("会议记录：" + "\n" + meetingRecord.getContent());
    }
}
